package com.uchengwang.ui.cm_ui.ucity.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.SchoolConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.base_ui.ui.MainActivity;
import com.uchengwang.ui.cm_ui.ucity.adapter.ChooseSchoolAdapter;
import com.uchengwang.ui.cm_ui.ucity.cityBean.SchoolBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.SchoolListBean;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.MapManager;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

/**
 * Created by U城网 on 2017/4/7.
 * 切换院校的界面
 */

public class SwitchSchoolActivity extends BaseWithTitleActivity{
    private TextView choose_current;//当前所在院校
    private RelativeLayout choose_near;//附近院校 点击查看更多
    private RecyclerView choose_rv;
    private PtrClassicFrameLayout mSwipeRefreshLayout;
    private RecyclerAdapterWithHF mAdapter;
    private ChooseSchoolAdapter adapter;
    private LinearLayout choose_current_ln;

    private int page = 1;//当前页  每页请求多少数据
    private int totlaPagers ;//总页数【服务器返回】
    private int type;//1选择学校 返回U城 刷新主页 3选择学校 将学校信息带回上个界面  4选择学校认证

    private boolean isAdd;//是否添加U城这个选项 true添加
    private int toType;//这个字段配合type=4 来使用  0 finish这个界面 1 跳转到MainActivity界面 2 跳转到clubMainActivity这个界面

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_choose_school);
        type = getIntent().getIntExtra(BaseConfig.TYPE, 1);
        toType = getIntent().getIntExtra(BaseConfig.PUB_TYPE, 0);
        isAdd = getIntent().getBooleanExtra(BaseConfig.IS_ACTIVI, false);
        setTitle("选择院校");

        setLeftListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toMain();
            }
        });



        initView();
    }

    private void initView() {
        mSwipeRefreshLayout = (PtrClassicFrameLayout) findViewById(R.id.mRefreshLayout);
        choose_current_ln = (LinearLayout) findViewById(R.id.choose_current_ln);
        choose_current = (TextView) findViewById(R.id.choose_current);
        choose_near = (RelativeLayout) findViewById(R.id.choose_near);
        choose_rv = (RecyclerView) findViewById(R.id.choose_rv);



        initData();

    }
    private void initData(){
        if (TextUtils.isEmpty(UChengApplication.schoolName)){
            choose_current_ln.setVisibility(View.GONE);
        }else {
            choose_current.setText(UChengApplication.schoolName);
        }

        if (type ==3 || type ==4){
            choose_current_ln.setVisibility(View.GONE);
        }

        choose_rv.setLayoutManager(new LinearLayoutManager(SwitchSchoolActivity.this));
        choose_rv.addItemDecoration(new DividerItemDecoration(SwitchSchoolActivity.this, DividerItemDecoration.VERTICAL));
        adapter = new ChooseSchoolAdapter(SwitchSchoolActivity.this, type);
        mAdapter = new RecyclerAdapterWithHF(adapter);
        choose_rv.setAdapter(mAdapter);

        mSwipeRefreshLayout.setAutoLoadMoreEnable(true);
        mSwipeRefreshLayout.setLoadMoreEnable(true);

        mSwipeRefreshLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                mSwipeRefreshLayout.autoRefresh(true);
            }
        }, 150);

        mSwipeRefreshLayout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                mSwipeRefreshLayout.loadMoreComplete(true);
                getSchools(true);
            }
        });

        mSwipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                page++;
                if (page <= totlaPagers){
                    //当前请求页大于总页数，不再请求
                    getSchools(false);
                } else {
                    mSwipeRefreshLayout.loadMoreComplete(false);
                }
            }
        });

    }

    private void getSchools(final boolean isRefresh){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, 20);
        HttpManager.newInstance(this).query(SchoolConfig.GetSchools.method_get, SchoolConfig.GetSchools.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                if (isRefresh){
                    mSwipeRefreshLayout.refreshComplete();
                }else {
                    mSwipeRefreshLayout.loadMoreComplete(true);
                }
                UCommonUtil.getUtil().showToast(SwitchSchoolActivity.this, "获取失败");
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                if (isRefresh){
                    mSwipeRefreshLayout.refreshComplete();
                }else {
                    mSwipeRefreshLayout.loadMoreComplete(true);
                }
                if (status != 0){
                    UCommonUtil.getUtil().showToast(SwitchSchoolActivity.this, msg);
                    return;
                }

                if (!TextUtils.isEmpty(data)){
                    SchoolListBean bean = JsonUtils.getManager().stringToBean(data, SchoolListBean.class);
                    if (bean != null){
                        totlaPagers = bean.getTotalPage();
                        List<SchoolBean> list = bean.getData();
                        if (list != null && list.size() >0){

                            if (isRefresh){
                                if (isAdd){
                                    SchoolBean schoolBean = new SchoolBean();
                                    schoolBean.setUniName("U城");
                                    schoolBean.setId(0);
                                    list.add(0, schoolBean);
                                }
                                adapter.setData(list);
                            }else {
                                adapter.getData().addAll(list);
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        toMain();
    }

    public void toMain(){
        //认证流程
        if (type ==4){
            Intent intent =null;
            //0 finish这个界面 1 跳转到MainActivity界面 2 跳转到clubMainActivity这个界面
            switch (toType){
                case 0:
                    break;
                case 1:
                    intent = new Intent(this, MainActivity.class);
                    intent.putExtra(BaseConfig.INDEX, 3);
                    break;
            }
            if (intent != null){
                startActivity(intent);
            }
        }
        this.finish();
    }

    @Override
    protected void onStop() {
        super.onStop();
        MapManager.getMapManager().stopLocation();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MapManager.getMapManager().desLocation();
    }
}
